/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package ms.jasim.model.event.impl;

import java.util.Collection;

import ms.jasim.framework.IJasimAppContext;
import ms.jasim.framework.SolutionSimulator.SimulatorArg;
import ms.jasim.model.ModelPackage;
import ms.jasim.model.event.CompositePrecondition;
import ms.jasim.model.event.CompositeType;
import ms.jasim.model.event.EventPackage;
import ms.jasim.model.event.EventPrecondition;
import ms.jasim.model.impl.ModelObjectImpl;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;
import org.eclipse.emf.ecore.util.EObjectResolvingEList;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Composite Precondition</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link ms.jasim.model.event.impl.CompositePreconditionImpl#getType <em>Type</em>}</li>
 *   <li>{@link ms.jasim.model.event.impl.CompositePreconditionImpl#getCondition <em>Condition</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class CompositePreconditionImpl extends ModelObjectImpl implements CompositePrecondition {
	/**
	 * The default value of the '{@link #getType() <em>Type</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getType()
	 * @generated
	 * @ordered
	 */
	protected static final CompositeType TYPE_EDEFAULT = CompositeType.AND;

	/**
	 * The cached value of the '{@link #getType() <em>Type</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getType()
	 * @generated
	 * @ordered
	 */
	protected CompositeType type = TYPE_EDEFAULT;

	/**
	 * The cached value of the '{@link #getCondition() <em>Condition</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getCondition()
	 * @generated
	 * @ordered
	 */
	protected EList<EventPrecondition> condition;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected CompositePreconditionImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return EventPackage.Literals.COMPOSITE_PRECONDITION;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public CompositeType getType() {
		return type;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setType(CompositeType newType) {
		CompositeType oldType = type;
		type = newType == null ? TYPE_EDEFAULT : newType;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, EventPackage.COMPOSITE_PRECONDITION__TYPE, oldType, type));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<EventPrecondition> getCondition() {
		if (condition == null) {
			condition = new EObjectContainmentEList<EventPrecondition>(EventPrecondition.class, this, EventPackage.COMPOSITE_PRECONDITION__CONDITION);
		}
		return condition;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 */
	public boolean conditionMatch(IJasimAppContext context, SimulatorArg simulatorInfo) {
		boolean result = false;
		for (EventPrecondition cond: getCondition()) {
			result = cond.conditionMatch(context, simulatorInfo);
			if ( (getType() == CompositeType.AND && !result) || result)
				break;
		}
		return result;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case EventPackage.COMPOSITE_PRECONDITION__CONDITION:
				return ((InternalEList<?>)getCondition()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case EventPackage.COMPOSITE_PRECONDITION__TYPE:
				return getType();
			case EventPackage.COMPOSITE_PRECONDITION__CONDITION:
				return getCondition();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case EventPackage.COMPOSITE_PRECONDITION__TYPE:
				setType((CompositeType)newValue);
				return;
			case EventPackage.COMPOSITE_PRECONDITION__CONDITION:
				getCondition().clear();
				getCondition().addAll((Collection<? extends EventPrecondition>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case EventPackage.COMPOSITE_PRECONDITION__TYPE:
				setType(TYPE_EDEFAULT);
				return;
			case EventPackage.COMPOSITE_PRECONDITION__CONDITION:
				getCondition().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case EventPackage.COMPOSITE_PRECONDITION__TYPE:
				return type != TYPE_EDEFAULT;
			case EventPackage.COMPOSITE_PRECONDITION__CONDITION:
				return condition != null && !condition.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (Type: ");
		result.append(type);
		result.append(')');
		return result.toString();
	}

	
	@Override
	public Object getValue(String name) {
		for (EventPrecondition pred: getCondition())
			if (pred.hasValue(name)) 
				return pred.getValue(name);
		return null;
	}

	
	@Override
	public boolean hasValue(String name) {
		for (EventPrecondition pred: getCondition())
			if (pred.hasValue(name)) 
				return true;
		return false;
	}

	
	@Override
	public void setValue(String name, Object value) {
		
	}

} //CompositePreconditionImpl
